<!-- 图标选择器
<select-icon v-model=[绑定变量] :data=[选项数据] />
-->
<template>
  <el-popover
    v-model="popoverVisible"
    placement="bottom-start"
    popper-class="pupop-select-icon"
    trigger="click"
    :disabled="disabledSelected"
  >
    <!-- 弹出框内容区 -->
    <div class="search">
      <el-input
        v-model="searchKey"
        placeholder="搜索"
        size="small"
      >
        <i slot="prefix" class="el-input__icon el-icon-search" />
      </el-input>
    </div>
    <div class="scroll_view">
      <el-scrollbar
        :native="false"
        :noresize="false"
      >
        <!-- 图标项 -->
        <div
          v-for="item in displayOptions"
          :key="item"
          :class="['icon-item',{ 'is-active': isActive(item) }]"
          @click="onClickSelected(item)"
        >
          <el-tooltip placement="top">
            <div slot="content">
              {{ item }}
            </div>
            <svg-icon :icon-class="item" />
          </el-tooltip>
        </div>
      </el-scrollbar>
    </div>
    <!-- 页面显示内容区 -->
    <template slot="reference">
      <div
        :class="{
          'mod-select-icon': 1,
          'is-opened': popoverVisible,
          'is-active': value,
          'is-disabled': disabledSelected
        }"
      >
        <!-- 显示图标 -->
        <div class="icon-item">
          <svg-icon v-if="value" :icon-class="value" />
          <i v-else class="el-icon-plus" />
        </div>
        <!-- 清空按钮 -->
        <div v-show="value && !disabledSelected" class="btn-clear">
          <i class="el-icon-close" @click.stop="onClickClear()" />
        </div>
      </div>
    </template>
  </el-popover>
</template>

<script>
export default {
  // 设置绑定参数
  model: {
    prop: 'value',
    event: 'selected'
  },
  props: {
    disabled: Boolean,
    // 接收绑定参数 - 图标类名
    value: {
      type: null,
      validator: val => typeof val === 'string' || val === null,
      required: true
    }
  },
  data() {
    const req = require.context('@/icons/svg', false, /\.svg$/)
    const requireAll = requireContext => requireContext.keys()
    const re = /\.\/(.*)\.svg/
    const svgIcons = requireAll(req).map(i => {
      return i.match(re)[1]
    })
    return {
      // 弹出框显示状态
      popoverVisible: false,
      searchKey: '',
      options: svgIcons
    }
  },
  computed: {
    disabledSelected() {
      if (this.disabled) return true
      return this.$parent.form ? this.$parent.form.disabled : false
    },
    displayOptions() {
      if (!this.searchKey) {
        return this.options
      }
      return this.options.filter(item => item.includes(this.searchKey))
    }
  },
  methods: {
    // 是否为当前已选项
    isActive(item) {
      return this.value === item
    },
    // 选中图标
    onClickSelected(item) {
      this.$emit('selected', item)
      this.popoverVisible = false
    },
    // 清空选项
    onClickClear() {
      this.$emit('selected', '')
    }
  }
}
</script>

<style lang="scss">
.mod-select-icon {
  $size: 30px;
  $col-count: 8;
  $row-count: 4;
  $scope: 5px;

  position: relative;
  display: inline-block;
  width: $size + 2px;
  height: $size + 2px;
  border: 1px dashed #c0c4cc;
  border-radius: 5px;
  text-align: center;
  cursor: pointer;
  outline: none;

  > .icon-item {
    width: $size;
    height: $size;
    line-height: $size;
    text-align: center;
    > svg {
      display: block;
      width: 100%;
      height: 100%;
      line-height: $size - ($scope * 2);
      color: #24292e;
      background-color: #fff;
    }
    > i.el-icon-plus {
      width: 100%;
      line-height: $size;
      font-size: 16px;
      font-weight: bold;
      color: #c0c4cc;
      cursor: inherit;
    }
  }
  .btn-clear {
    width: 0;
    height: 0;
    border-width: ($size / 2) 0 0 ($size / 2);
    border-style: solid;
    border-color: #c0c4cc transparent transparent transparent;
    position: absolute;
    top: 0;
    right: 0;
    cursor: pointer;
    > i.el-icon-close {
      position: absolute;
      top: -($size / 2);
      right: 0;
      color: #24292e;
      font-size: .7em;
      &:hover { color: darken(#24292e, 5%); }
    }
  }
  &.is-opened, &:hover { border-color: #fff; }
  &.is-active {
    border-style: solid;
    border-radius: 0;
    > .icon-item {
      padding: $scope;
      > svg {
        color: #24292e;
      }
    }
  }
  &.is-disabled {
     border-color: rgba(0,0,0,0.1);
     &:hover { border-color: rgba(0,0,0,0.1) !important; }
     cursor: not-allowed;
     > .icon-item {
      > svg {
        color: rgba(0,0,0,0.1);
      }
      > i.el-icon-plus {
        color: rgba(0,0,0,0.1);
      }
    }
  }
  @at-root .el-popover.el-popper.pupop-select-icon {
    display: block;
    padding: 0 10px;
    width: ($size + $scope * 2) * $col-count + 22;
    height: ($size + $scope * 2) * $row-count + 52px;
    .search {
      height: 52px;
      padding: 10px 0;
    }
    .scroll_view {
      height: ($size + $scope * 2) * $row-count;
      > .el-scrollbar {
        height: 100%;
        > .el-scrollbar__wrap {
          overflow-x: hidden;
        }
      }
    }
    .icon-item {
      float: left;
      width: $size;
      height: $size;
      padding: $scope;
      margin: $scope;
      text-align: center;
      cursor: pointer;
      background: #fff;
      &:hover { background-color: #c0c4cc;}
      &.is-active {
        background-color: #c0c4cc;
      }
      > svg {
        display: block;
        width: 100%;
        height: 100%;
        color: #24292e;
      }
    }
  }
}
</style>
